package fuck.popup.actions;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

public class SelectTextAction implements IObjectActionDelegate {

	private Shell shell;

	private IWorkbenchPart targetPart;

	/**
	 * Constructor for Action1.
	 */
	public SelectTextAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
		this.targetPart = targetPart;
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		String title = targetPart.getTitle();
		ISelection selection = targetPart.getSite().getSelectionProvider().getSelection();
		if (!(selection instanceof ITextSelection)) {
			return;
		}
		
		
		//textSelection.getEndLine();光标位置
//		targetPart.getSite().getD

		ITextSelection textSelection = (ITextSelection) selection;
		
		String text = textSelection.getText();
		if (text == null || text.length() == 0) {
			return;
		}
		
		
		int endLine = textSelection.getEndLine();
		int startLine = textSelection.getStartLine();
		int offset = textSelection.getOffset();
		System.out.println(text + "(title = " + title + ":" + startLine + ":" + endLine + ":" + offset + ")");
		//org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor
		CompilationUnitEditor cue = (CompilationUnitEditor)targetPart;
//		cue.getViewer();
//		targetPart.getSite().getPage().getActiveEditor().getEditorInput();
//		targetPart.getSite().getPage();
		// 这里怎么获得选中文本的颜色？如果选中了多个段，如何循环得到每段的颜色？
		MessageDialog.openInformation(shell, "Cat", "New Action was executed.");
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}
}
